package com.sahyadri.web.bean;

import java.io.Serializable;


import com.sahyadri.web.enums.SortColumn;
import com.sahyadri.web.enums.SortOrder;


public class BaseSearchCriteria implements Serializable {

	private static final long serialVersionUID = 8958838154543499526L;
	private String loginId;
	private SortColumn sortColumn = SortColumn.CODE;
	private SortOrder sortOrder = SortOrder.DESC;
	private int startIndex;
	private int pageSize;

	public SortColumn getSortColumn() {
		return sortColumn;
	}

	public void setSortColumn(SortColumn sortColumn) {
		this.sortColumn = sortColumn;
	}

	public SortOrder getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(SortOrder sortOrder) {
		this.sortOrder = sortOrder;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	/**
	 * Utility method used to convert starting index and page size combination
	 * into page number. If page size is 0, defaults to page 1 otherwise
	 * calculates pages size based on startingIndex and pageSize
	 * 
	 * @return The page number based on startingIndex and pageSize or 1 if
	 *         pageSize is not set properly (0).
	 */
	public int getPageNumber() {

		if (this.pageSize == 0) {
			return 1;
		}

		return this.startIndex / pageSize + 1;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("BaseSearchCriteria [loginId=");
		builder.append(loginId);
		builder.append(", pageSize=");
		builder.append(pageSize);
		builder.append(", sortColumn=");
		builder.append(sortColumn);
		builder.append(", sortOrder=");
		builder.append(sortOrder);
		builder.append(", startIndex=");
		builder.append(startIndex);
		builder.append("]");
		return builder.toString();
	}

}
